var currentTreeNode = null;

$(document).ready(function() {
    initTree();
});

/***
 * 加载机构分类
 * */
function initTree() {
    $("#Tree").html("");
    listTree.load("Tree",{});
}

/***
 * 点击机构分类，加载功能树
 * */
function treeselected(e) {
    var e = e || window.event;
    $("#orgNameText").text("  --  "+ e.name);
    currentTreeNode = e.node;
    authtree = layui.authtree;
    load_function(authtree, currentTreeNode);

    authtree.on('change(lay-check-auth)', function() {
        var funLastChecked = authtree.getLastChecked('#functionTree'),//获取最新选中
            funLastNotChecked = authtree.getLastNotChecked('#functionTree');// 获取最新取消
        if(funLastChecked.length > 0){
            saveEmpowerFun(funLastChecked, authtree);
        }
        if(funLastNotChecked.length > 0){
            delEmpowerFun(funLastNotChecked, authtree);
        }
    });
}

/*
* @author: glx
* @description: 加载功能列表
* @date: 2018/12/27 11:11
* @param
* @return
*/
function load_function(authtree){
    var dataObj = {};
    if(currentTreeNode){
        //查询授权功能
        ActHttpRequest('/sys/cate/function/list', {'functionCate' : currentTreeNode.id, 'functionState': 1}, function(returnData){
            if(returnData.OK === "OK"){
                dataObj = returnData;
            }else{
                layer.alert(returnData.message);
            }
        });
    }
    authtree.render('#functionTree', reload_saved_data(dataObj,"parentId","functionId","functionName"), {
        inputname: 'authids[]'
        ,layfilter: 'lay-check-auth'
        ,autowidth: true
    });
}
/*
* @author: wj
* @description: 保存
* @date: 2019/1/16 17:04
* @param
* @return
*/
function saveEmpowerFun(funLastChecked, authtree){
    var checked = funLastChecked,
        functionId = [];
    for (var i = 0; i < checked.length; i++) {
        var curretnNode = eval("("+checked[i]+")");
        functionId.push(curretnNode.functionId);
    }
    var formData = {
        'functionIds': functionId.join(),
        'functionCate': currentTreeNode.id
    };
    ActHttpRequest("/sys/cate/function/save", formData, function (data) {
        if (data.OK === "OK") {
            layer.msg("操作成功！", {icon : 1,time : 1000});
            load_function(authtree);
        } else {
            layer.msg(data.message, {icon : 1,time : 1000});
        }
    });
}
/*
* @author: wj
* @description: 删除
* @date: 2019/1/16 17:04
* @param
* @return
*/
function delEmpowerFun(funLastNotChecked, authtree){
    var checked = funLastNotChecked,
        functionId = "";
    for (var i = 0; i < checked.length; i++) {
        var everyFun = eval("("+checked[i]+")");
        functionId = functionId + "," + everyFun.functionId;
    }
    var saveData = {
        'functionCate': currentTreeNode.id,
        'functionIds': functionId.substring(1)
    };
    ActHttpRequest("/sys/cate/function/delUpdateSysCateFunction", saveData, function (data) {
        if (data.OK === "OK") {
            layer.msg("操作成功！", {icon : 1,time : 1000});
            load_function(authtree);
        }else{
            layer.alert(data.message, {icon : 1,time : 1000});
        }
    });
}

/*
* @author: wj
* @description: 加载数据
* @date: 2018/12/17 16:01
* @param
* @return
*/
function reload_saved_data(data,pid,id,name){
    var items={};
    if(data && data.List){
        for(var i=0;i<data.List.length;i++){
            var obj = data.List[i];
            var parentId="root"
            if(obj[pid] && obj[pid]!="" && obj[pid]!="0"){
                parentId = obj[pid]
            }
            var jo={
                "id" : obj[id],
                "value" : obj[id],
                "name" : obj[name],
                "parentId" : parentId,
                "node": obj,
                "checked" : obj["checked"]
            }
            if(items[parentId]){
                items[parentId].push(jo);
            }else{
                items[parentId] = [];
                items[parentId].push(jo);
            }
        }
    }

    return formatTree(items, "root");
}
/**
 * 递归每个节点
 */
function formatTree(items, parentId) {
    var result = [];

    if (!items[parentId]) {
        return result;
    }
    for (var i=0;i<items[parentId].length;i++){
        var t=items[parentId][i];
        t["list"] = formatTree(items, t.id)
        result.push(t);
    }
    return result;
}